package com.ynet.middleground.oss.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

/**
 * @author liulx
 * @description 错误码工具类
 * @date 2019-11-30 16:23
 */
@Configuration
@PropertySource("classpath:error-code.properties")
public class ErrorCodeUtil {

    @Autowired
    private Environment environment;

    /**
     * Get value by key
     *
     * @param key key to get value from properties file
     * @return value
     */
    public String getValueOriginal(String key) {
        return environment.getProperty(key);
    }

    /**
     * 根据key获取描述值
     * 
     * @param key code值
     * @return 错误码对应描述
     */
    public String getValue(String key) {
        return getValue(key, null);
    }

    /**
     * 根据key获取描述
     * 
     * @param key code值
     * @param args 占位符替换值
     * @return 错误码对应描述
     */
    public String getValue(String key, Object... args) {
        return getValueWithPrefix("", key, args);
    }

    /**
     * 根据key获取描述
     * 
     * @param prefix 前缀
     * @param key code 值
     * @param args 占位符替换值
     * @return 错误码对应描述
     */
    public String getValueWithPrefix(String prefix, String key, Object... args) {
        String property = environment.getProperty(key);
        if (StringUtils.isBlank(property)) {
            return "";
        }
        return prefix + String.format(property, args);
    }

}
